#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#define NUM_WRITER  10
#define NUM_TVS     100
struct timeval* ktvs = NULL;
int kindex = 0;
pthread_mutex_t klock;
static void* func_write(void* arg);
int main()
{
    pthread_mutex_init(&klock, NULL);

    ktvs = malloc(sizeof(struct timeval)*NUM_TVS);
    assert(NULL != ktvs);

    pthread_t pth;
    int ret;
    int i;
    for(i=0; i<NUM_WRITER; i++) {
        ret = pthread_create(&pth, NULL, func_write, NULL);
        assert(0 == ret);
    }

    getchar();
    return 0;
}


void write_tv(struct timeval* tv)
{
    pthread_mutex_lock(&klock);
    ktvs[kindex] = *tv;
    kindex = (kindex+1)%NUM_TVS;
    pthread_mutex_unlock(&klock);
}


void* func_write(void* arg)
{
    struct timeval tv;
    while(1) {
        gettimeofday(&tv, NULL);
        write_tv(&tv);
        usleep(10*1000);
    }

    return NULL;
}
